package com.example.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.FileSearch.SQLiteFileRecorder;
import com.example.Service.FileSearchService;
import com.example.training.R;

import java.util.TimerTask;

public class FileSearchActivity extends Activity
        implements TextView.OnEditorActionListener, TextWatcher {

    private Activity _activity;
    private String _filter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filesearch_activity);
        _activity = this;
        startService(new Intent(this, FileSearchService.class));
        EditText txtFilter = (EditText)findViewById(R.id.text_search_filter);
        txtFilter.setOnEditorActionListener(this);
        txtFilter.addTextChangedListener(this);
    }

    //OnEditorActionListener
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent arg2) {
        if(actionId == EditorInfo.IME_ACTION_SEARCH)
        {
          String filter = view.getText().toString().trim();
          ListView lsvFiles = (ListView)findViewById(R.id.list_files);
          String [] items = new String[0];
          if(filter.length() > 0){
              SQLiteFileRecorder recorder = new SQLiteFileRecorder(this);
              items = recorder.SearchFile(filter);
          }
          lsvFiles.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
        }
        return false;
    }

    //TextWatcher
    @Override
    public void beforeTextChanged(CharSequence s, int arg1, int arg2,int arg3) {}
    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void onTextChanged(CharSequence s, int arg1, int arg2,int arg3) {
        _filter = s.toString().trim();
    //            _timer.cancel();
    //            _timer = new Timer(true);
    //            _timer.schedule(GetTimerTask(), 1000, 1);
    }

    private TimerTask GetTimerTask(){
        return new TimerTask(){
            @Override
            public void run() {
                final ListView lsvFiles = (ListView)_activity.findViewById(R.id.list_files);
                String [] items = new String[0];
                if(_filter.length() > 0){
                    SQLiteFileRecorder recorder = new SQLiteFileRecorder(_activity);
                    items = recorder.SearchFile(_filter);
                }
                final String [] updateItems = items;
                runOnUiThread(new Runnable(){
                    @Override
                    public void run() {
                        lsvFiles.setAdapter(new ArrayAdapter<String>(_activity, android.R.layout.simple_list_item_1, updateItems));
                    }
                 });
            }
        };
    }

}
